package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.dto.condition.MerchantSearchCondition;
import com.treasure.mall.biz.dto.live.ApplyLiveDTO;
import com.treasure.mall.biz.dto.order.OrderStatisticsDTO;
import com.treasure.mall.biz.dto.user.ModifyShopDTO;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.live.LiveRoomService;
import com.treasure.mall.biz.service.core.statistics.OrderStatisticsService;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.biz.service.core.user.MerchantShopFollowService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.biz.vo.user.ApplyMerchantVO;
import com.treasure.mall.biz.vo.user.MerchantVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.ienum.live.LiveEnums;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/12 20:02
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.zbqmallapi.controller Inc. All rights reserved.
 */
@RestController
@RequestMapping("/merchant")
@AccessControl(needLogin = true)
@Api(tags = "商家模块")
public class MerchantController {

    @Resource
    private MerchantService merchantService;
    @Resource
    private LiveRoomService liveRoomService;
    @Resource
    private MerchantShopFollowService followService;
    @Resource
    private OrderStatisticsService orderStatisticsService;

    /**
     * 获取店铺信息
     *
     * @param user
     * @param condition
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("获取店铺信息")
    @GetMapping("/shopList")
    @AccessControl
    public ApiResult shopList(@CurrentUser UserPO user, MerchantSearchCondition condition,
                              @RequestParam(required = false, defaultValue = "1") int page,
                              @RequestParam(required = false, defaultValue = "20") int size
    ) {
        condition.setStatus(LiveEnums.AuditStatus.SUCCESS.getCode());
        Page<MerchantPO> pageData = merchantService.findAll(page, size, condition);
        PageDataVO<MerchantVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageSize(size);
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageCount(pageData.getTotalPages());
        pageDataVO.setTotal(pageData.getTotalElements());
        pageDataVO.setRows(merchantService.listVO(pageData.getContent(), user != null ? user.getUserId() : null));
        return ApiResult.ok(pageDataVO);
    }


    /**
     * 申请开店
     *
     * @param user
     * @param dto
     * @return
     */
    @ApiOperation("申请开店")
    @PostMapping("/userMerchantRequests")
    public ApiResult userMerchantRequests(@CurrentUser UserPO user, @RequestBody ModifyShopDTO dto) {
        merchantService.applyShop(user.getUserId(), dto);
        return ApiResult.ok();
    }

    /**
     * 修改店铺信息
     *
     * @param user
     * @param dto
     * @param dto
     * @return
     */
    @ApiOperation("修改店铺信息")
    @PutMapping("/userMerchantRequests/modify")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Object userMerchantRequestsModify(@CurrentUser UserPO user, @RequestBody ModifyShopDTO dto) {
        merchantService.modifyShopData(user.getUserId(), dto);
        return ApiResult.ok();
    }


    /**
     * 必须已支付了，才能获取到数据
     *
     * @param user
     * @return
     */
    @ApiOperation("获取店铺审核信息")
    @GetMapping("/userMerchantRequests/current")
    public Object userMerchantRequestsByCurrent(@CurrentUser UserPO user) {
        ApplyMerchantVO vo = merchantService.findApplyShopDataByUserId(user.getUserId());
        if (vo != null) {
            return ApiResult.ok(vo);
        }
        return ApiResult.error(ResultCodeEnum.NOT_AUTHORITY);
    }

    /**
     * 申请直播间
     *
     * @param dto
     * @return
     */
    @ApiOperation("申请直播间")
    @PostMapping("/merchantLiveRoomRequests")
    public Object merchantLiveRoomRequests(ApplyLiveDTO dto) {
        liveRoomService.apply(dto);
        return ApiResult.ok();
    }

    /**
     * 获取店铺信息
     *
     * @param merchantId
     * @return
     */
    @ApiOperation("根据获取店铺信息")
    @GetMapping("/userMerchantRequests/{id}/findFromUser")
    @AccessControl
    public ApiResult merchantsFindFromUser(@CurrentUser UserPO user, @PathVariable("id") Long merchantId) {

        MerchantVO vo = merchantService.findShopDataByMerchantId(merchantId, user != null ? user.getUserId() : null);
        if (vo == null) {
            return ApiResult.errorWith(ResultCodeEnum.ERROR, "店铺不存在");
        }
        return ApiResult.ok(vo);
    }

    /**
     * 关注店铺
     *
     * @param user
     * @param merchantId
     * @return
     */
    @ApiOperation("关注店铺")
    @GetMapping("/follow/{id}")
    public ApiResult follow(@CurrentUser UserPO user, @PathVariable("id") Long merchantId) {
        followService.follow(user.getUserId(), merchantId);
        return ApiResult.ok();
    }

    /**
     * 取消关注
     *
     * @param user
     * @param merchantId
     * @return
     */
    @ApiOperation("取消关注")
    @GetMapping("/unfollow/{id}")
    public ApiResult unfollow(@CurrentUser UserPO user, @PathVariable("id") Long merchantId) {
        followService.unfollow(user.getUserId(), merchantId);
        return ApiResult.ok();
    }

    /**
     * 商家中心的一些统计
     *
     * @param userPO
     * @return
     */
    @ApiOperation("商家中心的一些统计")
    @GetMapping("/order/statistics")
    public ApiResult orderStatistics(@CurrentUser UserPO userPO) {
        OrderStatisticsDTO statisticsDTO = orderStatisticsService.orderStatistics(userPO.getMerchant());
        return ApiResult.ok(statisticsDTO);
    }
}
